
#import "../Alexandra.h"
#import "PMCompose.h"
#import "preferences.h"
#import <dbkit/DBTableView.h>

@implementation PreferencesManager(Compose)

//-----------------------------------------------------------
// R/W 
//-----------------------------------------------------------

- readPostingPrefs;
	{
	KVPair		*pair=[[KVPair alloc] init];
	char		defname[200];
	const char	*d;
	int			i,n=[NXApp defaultIntValue:DEFAULT_XHEADER_COUNT];
	MiscList	*rows;
	
	[pQuotingPrefix setStringValue:[NXApp defaultValue:DEFAULT_QUOTING_PREFIX]];
	[pAppendSig setState:[NXApp defaultBoolValue:DEFAULT_APPEND_SIG]?1:0];

	rows=[[MiscList alloc] init];
	for(i=0;i<n;i++)
		{
		sprintf(defname,DEFAULT_XHEADERS,i+1);
		d=[NXApp defaultValue:defname];
		if(d)
			[rows addObject:[[KVPair alloc] init:d delimiter:':']];
		else
			[rows addObject:pair];
		}
	[pHeaderController empty:nil];
	[pHeaderController addRowsFrom:rows];

	rows=[rows free];
	pair=[pair free];
	return self;
	}
	
	
- writePostingPrefs
	{
	MiscList *pHeaders=[pHeaderController rows];
	KVPair	*pair;
	int		i=1;
	int		n=[NXApp defaultIntValue:DEFAULT_XHEADER_COUNT]+1;
	char	defname[200], defvalue[200];

	[pTableView endEditing];

	[NXApp setDefault:DEFAULT_QUOTING_PREFIX to:[pQuotingPrefix stringValue]];
	[NXApp setDefault:DEFAULT_APPEND_SIG toBool:(BOOL)[pAppendSig state]];

	for(pair=[pHeaders setFirstObject]; pair; pair=[pHeaders setNextObject])
		if(![pair isNull])
			{
			sprintf(defvalue, "%s: %s",[pair key],[pair value]);
			sprintf(defname,DEFAULT_XHEADERS,i++);
			[NXApp setDefault:defname to:defvalue];
			}
	
	for(;i<n;i++)
		{
		sprintf(defname,DEFAULT_XHEADERS,i);
		[NXApp setDefault:defname to:" : "];
		}
	
	return self;	
	}


//-----------------------------------------------------------
// THAT'S IT
//-----------------------------------------------------------

@end
